package com.frxuexi.report.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.UiState
import com.frxuexi.report.R
import com.frxuexi.report.ReportEntity
import com.frxuexi.report.ReportIntent
import com.frxuexi.report.ReportViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

data class ReportItemEntity(val type:Int,val name:String,val title:String)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReportPage(navHostController: NavHostController,vm: ReportViewModel = hiltViewModel()) {

    var list = remember {
        SnapshotStateList<ReportEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.clear()
                        Log.i("===",it.data.toString())
                        list.addAll(it.data as Collection<ReportEntity>)
                    }
                    is UiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }

        vm.sendIntent(ReportIntent.getReports(mapOf("userId" to 1, "page" to 1)))
    })
    var isShow by remember {
        mutableStateOf(false)
    }

    var dialogList= arrayOf(
        ReportItemEntity(1,"1","日报"),
        ReportItemEntity(2,"7","周报"),
        ReportItemEntity(3,"30","月报"),
        ReportItemEntity(4,"季度","季度报表"),
        ReportItemEntity(5,"业绩","业绩报表"),
        ReportItemEntity(6,"营业","营业财报")
    )

    Scaffold(floatingActionButton = {
        Icon(painter = painterResource(id = R.drawable.ic_action_add), contentDescription = "",
            Modifier.clickable { isShow=true })
    }) {
        Column (Modifier.padding(it)){
            MyTitleBar(title = "工作日志", navHostController = navHostController)
            LazyColumn(Modifier.padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp), content = {
                items(list){
                    ReportItem(it)
                }
            })
        }
    }

    if(isShow){
        AlertDialog(onDismissRequest = { isShow=false }) {
            Column(
                Modifier
                    .background(Color.White, RoundedCornerShape(20.dp))
                    .padding(20.dp)) {
                Text(text = "新建汇报", fontWeight = FontWeight.Bold)
                Spacer(modifier = Modifier.height(10.dp))
                LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp), columns = GridCells.Fixed(3), content = {
                    items(dialogList){
                        DialogItem(entity = it,navHostController){
                            isShow=false
                        }
                    }
                })
            }
        }
    }
}

/**
 * 弹框的item
 */
@Composable
fun DialogItem(entity: ReportItemEntity,navHostController: NavHostController,onClick:()->Unit){
    Column(Modifier.clickable {
        val json = Gson().toJson(entity)
        navHostController.navigate(RouterPath.COMMIT_PAGE+"/$json")
        onClick()
    }) {
        Column(modifier = Modifier
            .size(50.dp)
            .background(colorResource(id = R.color.lightblue), CircleShape)
            .padding(6.dp)
            .border(1.dp, Color.Blue, RoundedCornerShape(5.dp))
            .background(Color.White, RoundedCornerShape(5.dp)), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = entity.name)
        }
        Text(text = entity.title)
    }
}

/**
 * 日报列表item
 */
@Composable
fun ReportItem(entity: ReportEntity){
      Column(verticalArrangement = Arrangement.spacedBy(5.dp)) {
          Row(modifier = Modifier.height(50.dp), verticalAlignment = Alignment.CenterVertically) {
              Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "",
                  modifier = Modifier
                      .size(50.dp)
                      .clip(CircleShape))
              val type=if(entity.reportType==1) "日报" else if(entity.reportType==2) "周报" else if(entity.reportType==3) "月报"
              else if(entity.reportType==4) "季度" else if(entity.reportType==5) "业绩" else "营业"
              Spacer(modifier = Modifier.width(5.dp))
              Text(text = type)
          }
          Text(text = entity.reportNowTitle)
          Text(text = entity.reportNowValue)
          Text(text = entity.reportNextTitle)
          Text(text = entity.reportNextValue)
          val time = SimpleDateFormat("yyyy年MM月dd日").format(entity.reportTime)
          Text(text = "$time 发布")
      }
}